/**   
* @Title: SearchHistoryImpl.java 
* @author zengshuai shi   
* @date 2015年8月19日 下午3:34:03 
*/
package com.shanghaionstar.sqlite;

import android.content.Context;

import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.sqlite.entities.PoiSearchHistory;
import com.onstar.cn.sqlite.xutil.db.sqlite.Selector;
import com.onstar.cn.sqlite.xutil.db.sqlite.WhereBuilder;
import com.onstar.cn.sqlite.xutil.exception.DbException;

import java.util.ArrayList;
import java.util.List;

/** 
* @ClassName: SearchHistoryImpl 
* @Description: TODO
* @author zengshuai shi
* @date 2015年8月19日 下午3:34:03 
*/
public class SearchHistoryImpl extends BaseSqlImp {
	/** 
	* @Description: TODO
	* @author zengshuai shi
	* @date 2015年8月19日 下午3:35:04 
	*/
	public SearchHistoryImpl(Context context) {
		super(context);
	}
	
	/**
	 * 
	* @Title: deleteDatabase 
	* @Description: 超过20条删除
	* @param
	* @return void    返回类型 
	* @throws
	 */
	public void deleteDatabase() {
		int id = getId();
		if(id != -1){
			try {
				mDbUtils.deleteById(PoiSearchHistory.class, id);
			} catch (DbException e) {
				e.printStackTrace();
			}
		}
	}
	
	public int getId(){
		List<PoiSearchHistory> list = null;
		try {
			list = mDbUtils.findAll(Selector.from(PoiSearchHistory.class).orderBy("searchtime",true));
		} catch (DbException e) {
			e.printStackTrace();
		}
//				getAll(null, " 1=1 ", null, null, null, " searchtime DESC ", null);
		if(list.size()>20){
			return list.get(0).getId();
		}else{
			return -1;
		}
	}
	
	/**
	 * 
	* @Title: saveSearchHistory 
	* @Description: 保存搜索历史
	* @param @param P_info    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void saveSearchHistory(PoiItemInfo P_info) {
		PoiSearchHistory info = new PoiSearchHistory();
		info.setName(P_info.getTitle());
		info.setSearchtime(System.currentTimeMillis());
		info.setLanguage(DeviceUtil.getLanguage());
		info.setAddress(P_info.getAddress());
		info.setLat(P_info.getLatitude());
		info.setLon(P_info.getLongitude());
		info.setCityname(P_info.getCityName());
		info.setPhone(P_info.getTelphone());
		try {
			mDbUtils.save(info);
		} catch (DbException e) {
			e.printStackTrace();
		}
//		insert(info);
		deleteDatabase();
	}
	
	public PoiItemInfo PoiSearchHistoryToPoiItemInfo(PoiSearchHistory P_info) {
		PoiItemInfo info = new PoiItemInfo();
		info.setTitle(P_info.getName());
		info.setAddress(P_info.getAddress());
		info.setLatitude(P_info.getLat());
		info.setLongitude(P_info.getLon());
		info.setCityName(P_info.getCityname());
		info.setTelphone(P_info.getPhone());
		return info;
	}
	
	/**
	 * 
	* @Title: updateSearchHistory 
	* @Description: 更新搜索的历史数据
	* @param @param P_info    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void updateSearchHistory(PoiItemInfo P_info) {
		PoiSearchHistory info = new PoiSearchHistory();
		info.setName(P_info.getTitle());
		info.setSearchtime(System.currentTimeMillis());
		info.setLanguage(DeviceUtil.getLanguage());
		info.setAddress(P_info.getAddress());
		info.setLat(P_info.getLatitude());
		info.setLon(P_info.getLongitude());
		info.setCityname(P_info.getCityName());
		info.setPhone(P_info.getTelphone());
		try {
			mDbUtils.saveOrUpdate(info);
		} catch (DbException e) {
			e.printStackTrace();
		}
//		update(info);
	}
	/**
	 * 
	* @Title: getSearchList 
	* @Description: 获取搜索历史记录
	* @param @return    设定文件 
	* @return List<PoiSearchHistory>    返回类型 
	* @throws
	 */
	public List<PoiSearchHistory> getSearchList() {
		try {
			return mDbUtils.findAll(Selector.from(PoiSearchHistory.class).orderBy("searchtime",true));
		} catch (DbException e) {
			e.printStackTrace();
		}
		return  new ArrayList<>();
//		getAll(null, null, null, null, null, " searchtime desc ", null);
	}
	
	//count : 分页查询    0,50  0:表示页数   50:表示每页的条数  格式为"0,10"
	public List<PoiSearchHistory> getMoreSearchList(String count){
//		return getAll(null, null, null, null, null, " searchtime desc ", count);
		try {
			return mDbUtils.findAll(Selector.from(PoiSearchHistory.class).orderBy("searchtime",true).limit(Integer.parseInt(count)));
		} catch (DbException e) {
			e.printStackTrace();
		}
		return  new ArrayList<>();
	}

	public void deleteAll(){
		try {
			mDbUtils.deleteAll(PoiSearchHistory.class);
		} catch (DbException e) {
			e.printStackTrace();
		}
	}
	
}
